<template>


  <el-dialog
    top="20px"
    :title="defaultValue.title"
    :lock-scroll="tableConfig.lockScroll"
    :append-to-body="tableConfig.appendToBody"
    :visible="tableConfig.show"
    @close="tableConfig.show = false"
    :show-close="tableConfig.close"
    :destroy-on-close="tableConfig.destroyOnClose"
    width="1100px">

    <el-form size="small" ref="formOpsPersonalPasswordRef" :rules="checkRule" :model="submitData" label-width="180px">


        <el-form-item label="密码分类" prop="type">
            <el-select v-model="submitData.type" placeholder="请选择" clearable >
              <el-option
                v-for="item in typeOpt"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="密码名称" prop="name">
            <el-input v-model="submitData.name" />
        </el-form-item>
        <el-form-item label="密码状态" prop="status">
          <el-select v-model="submitData.status" placeholder="请选择" clearable  >
            <el-option key="1" label="使用中" :value="1"/>
            <el-option key="2" label="已废弃" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="submitData.userName" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="niceName">
          <el-input v-model="submitData.niceName" />
        </el-form-item>
        <el-form-item label="密码1" prop="password1">
            <el-input v-model="submitData.password1" />
        </el-form-item>
        <el-form-item label="密码2" prop="password2">
            <el-input v-model="submitData.password2" />
        </el-form-item>
        <el-form-item label="密码3" prop="password3">
            <el-input v-model="submitData.password3" />
        </el-form-item>
        <el-form-item label="密码4" prop="password4">
            <el-input v-model="submitData.password4" />
        </el-form-item>
        <el-form-item label="详细信息" prop="description">
            <el-input v-model="submitData.description" type="textarea" :autosize="{minRows: 2}"/>
        </el-form-item>
        <el-form-item label="网址" prop="webUrl">
          <el-input placeholder="请输入内容" v-model="submitData.webUrl">
            <template slot="prepend">Http://</template>
          </el-input>
        </el-form-item>
        <el-form-item label="绑定电话" prop="phone">
            <el-input v-model="submitData.phone" />
        </el-form-item>
        <el-form-item label="安全问答" prop="security">
            <el-input v-model="submitData.security" type="textarea" :autosize="{minRows: 2}"/>
        </el-form-item>



    </el-form>


    <span slot="footer" class="dialog-footer">
      <el-button plain type="warning" @click="toSaveUpd"
                 v-if="defaultValue.saveUpdSelect !== 'select'">{{ defaultValue.title }}
                 </el-button>
      <el-button plain type="primary" @click="tableConfig.show = false">关闭</el-button>
    </span>

  </el-dialog>


</template>

<script>

import http from '@/utils/http'
import {clearInvalid} from "@/utils/utils"

export default {
  name: "OpsPersonalPasswordSaveUpd",
  data() {
    return {
      // 表格的默认值
      tableConfig: {
        // 关闭时销毁 Dialog 中的元素
        destroyOnClose: true,
        // 弹出框是否打开
        show: false,
        // 是否显示关闭按钮
        close: true,
        // 下面2个属性解决打开弹出框后没有在最上面显示的问题，导致看到的内容是一半的，需要先将滚动条移到最上面
        lockScroll: false,
        appendToBody: true,
      },
      // 默认值，进入页面后的值
      defaultValue: {
        // 弹出框标题
        title: '查看',
        // 保存还是修改or查看,取值有 select | save | upd
        saveUpdSelect: 'select'
      },
      // 提交的数据
      submitData:{
        id: null,
        userId: null,
        type: null,
        name: null,
        status: null,
        userName: null,
        niceName: null,
        password1: null,
        password2: null,
        password3: null,
        password4: null,
        description: null,
        webUrl: null,
        phone: null,
        security: null,
      },
      // 校验规则
      checkRule: {
        type: [{required: true, message: '请选择密码分类', trigger: 'change'}],
        name: [{required: true, message: '请输入密码名称', trigger: 'blur'}],
        status: [{required: true, message: '请选择密码状态', trigger: 'change'}],
      },
      // 类型字典
      typeOpt: [],
    }
  },
  methods: {
    /**
     * 初始化操作
     * @param operateType 操作类型,保存还是修改or查看,取值有 select \ save \ upd
     * @param obj 如果是修改和查看，则是详细数据,需要赋值
     */
    init(operateType, obj){
      this.defaultValue.saveUpdSelect = operateType
      this.clearOldData()
      this.getDictListPage()

      if (operateType === 'save'){
        this.defaultValue.title = '新增'
        this.submitData.status = 1

      } else if (operateType === 'upd'){
        this.defaultValue.title = '修改'
        // 要json处理下，否则会影响之前的值
        this.submitData = JSON.parse(JSON.stringify(obj))

      } else {
        // 可能查看
      }

      this.tableConfig.show = true
    },
    /**
     * 清理掉旧数据
     */
    clearOldData(){
      this.submitData = {
        id: null,
        userId: null,
        type: null,
        name: null,
        status: null,
        userName: null,
        niceName: null,
        password1: null,
        password2: null,
        password3: null,
        password4: null,
        description: null,
        webUrl: null,
        phone: null,
        security: null,
      }
    },
    /**
     * 是保存还是修改
     */
    toSaveUpd(){
      if (this.defaultValue.saveUpdSelect === 'save'){
        this.saveOpsPersonalPassword()

      } else if (this.defaultValue.saveUpdSelect === 'upd'){
        this.updOpsPersonalPassword()

      } else {

      }
    },
    /**
     * 保存
     */
    saveOpsPersonalPassword(){
      http.post("/opsPersonalPassword/saveOpsPersonalPassword", this.submitData).then(response => {
        let res = response.data;

        this.$message.success ("保存成功");
        this.$emit('getListPage', null)
        // 关闭弹出框,继续添加
        this.tableConfig.show = false

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 更新
     */
    updOpsPersonalPassword(){
      clearInvalid(this.submitData)
      http.post("/opsPersonalPassword/updOpsPersonalPassword", this.submitData).then(response => {
        let res = response.data;

        this.$message.success ("更新成功");
        this.$emit('getListPage', null)
        // 关闭弹出框
        this.tableConfig.show = false

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 分页查询字典
     */
    getDictListPage(){
      http.post("/opsSysDict/getListPage", {
        // 分页参数
        current: 1,
        size: 999,
        sysDictGroupId: "1961080365305667584",
      }).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        // 处理数据
        this.typeOpt = data.records

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
  }
}
</script>

<style scoped>

</style>


